package com.t.bricks.bussiness.service.ocr.impl;

import java.util.Base64;

import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.model.ocr.ConditionOcr;
import com.t.bricks.bussiness.service.ocr.OcrService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.file.ZipUtil;

import NSOCR.HCFG;
import NSOCR.HIMG;
import NSOCR.HOCR;

@Service
public class OcrServiceImpl implements OcrService {

	@Override
	public MsgEmity translate(ConditionOcr conditionOcr) {

		if (conditionOcr == null || conditionOcr.getsContent() == null
				|| "".equals(conditionOcr.getsContent())) {
			return new MsgEmity(false, "图片资源不能为空！", 8001);
		}

		MsgEmity me = ZipUtil.unZipStr(conditionOcr.getsContent());
		if (!me.isSuccess()) {
			return new MsgEmity(false, "图片资源解压缩失败！", 8002);
		}

		String content = me.getData();

		byte[] bytes = Base64.getDecoder().decode(content);

		HCFG CfgObj = new HCFG();
		HIMG ImgObj = new HIMG();
		HOCR OcrObj = new HOCR();

		int res = 0;

		res = NSOCR.Engine.Cfg_Create(CfgObj);

		res = NSOCR.Engine.Ocr_Create(CfgObj, OcrObj);

		res = NSOCR.Engine.Img_Create(OcrObj, ImgObj);

		// 创建关联初始化
		NSOCR.Engine.Engine_InitializeAdvanced(CfgObj, OcrObj, ImgObj);

		// 目标语言默认为简体中文
		String languageAsian = "Chinese_Simplified";
		if ("en".equals(conditionOcr.getsLang())) {
			languageAsian = "English";
		}
		res = NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
				"Languages/" + languageAsian, "1");

		StringBuilder val = new StringBuilder();

		if (conditionOcr.getiRemoveLines() == 1) {
			// 1代表生效 0 不生效
			val.append("1");
			// 清除所有线
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"PixLines/RemoveLines", val.toString());
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"PixLines/FindHorLines", val.toString());
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"PixLines/FindVerLines", val.toString());
			val.setLength(0);
		}

		if (conditionOcr.getiNoiseFilter() == 1) {
			// 1代表生效 0 不生效
			val.append("1");
			// 对图像进行噪声滤波器
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/NoiseFilter", val.toString());
			val.setLength(0);
		}

		if (conditionOcr.getiSkewAngle() == 1) {
			// 图像修复角度
			val.append("360");
			// 修复图像倾斜
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/SkewAngle", val.toString());
			val.setLength(0);
		}
		String scale = conditionOcr.getsScale();
		if (scale != null && !"".equals(scale) && !"0".equals(scale)
				&& "0.25,0.5,1.0,1.5,2.0,2.5,4.0".contains(scale)) {
			// 按倍数缩放 "0.25", "0.5", "1.0", "1.5", "2.0", "2.5", "4.0"
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/AutoScale", "0");
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/ScaleFactor", scale);
		} else {// 自动缩放
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/AutoScale", "1");
			NSOCR.Engine.Cfg_SetOption(CfgObj, NSOCR.Constant.BT_DEFAULT,
					"ImgAlizer/ScaleFactor", "1.0");
		}

		res = NSOCR.Engine.Img_LoadFromMemory(ImgObj, bytes, bytes.length);

		if (res > NSOCR.Error.ERROR_FIRST) {
			String msg = "图片资源加入OCR识别器失败！";
			if (res == NSOCR.Error.ERROR_NOIMAGE) {
				msg = "无可识别的图片资源！";
			}
			return new MsgEmity(false, msg, 8004);
		}

		res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_FIRST,
				NSOCR.Constant.OCRSTEP_ZONING - 1, NSOCR.Constant.OCRFLAG_NONE);

		if (res > NSOCR.Error.ERROR_FIRST) {
			return new MsgEmity(false, "图片资源无法提取内容！", 8005);
		}

		res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_ZONING,
				NSOCR.Constant.OCRSTEP_LAST, NSOCR.Constant.OCRFLAG_NONE);
		if (res > NSOCR.Error.ERROR_FIRST) {
			return new MsgEmity(false, "图片资源无法提取内容！", 8006);
		}

		StringBuffer text = new StringBuffer();
		int fmt = NSOCR.Constant.FMT_EDITCOPY;

		if (conditionOcr.getiFmt() == 1) {
			fmt = NSOCR.Constant.FMT_EXACTCOPY;
		}

		res = NSOCR.Engine.Img_GetImgText(ImgObj, text, fmt);
		if (res > NSOCR.Error.ERROR_FIRST) {
			return new MsgEmity(false, "图片资源文字提取失败！", 8007);
		}

		String result = text.toString();

		if ("".equals(result.trim())) {
			return new MsgEmity(false, "图片资源文字提取为空！", 8008);
		}

		return new MsgEmity(true, "提取成功", result);
	}

}
